﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Retirement_fundSwitchPremiumRedirection : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Requests requests = new Requests();
        string requestIdstr = (string)Request.QueryString["requestId"];
        int requestId = Convert.ToInt32(requestIdstr);

        Requests req = Requests.getRequest2(requestId);

        showSwitchSource(req);
        showSwitchTarget(req);
        showRedirectTarget(req);
    }

    private void showSwitchSource(Requests req)
    {
        int i = 0;
        foreach (RequestsFund requestsFund in req.requestFunds)
        {
            
            string fundType = requestsFund.type;
            if ("SWITCH_FROM" == fundType)
            {
                i++;
                if (i == 1)
                {
                    ddl_sourceFund1.SelectedValue = requestsFund.code;
                    if (requestsFund.percentage == 0)
                    {
                        tb_sourceFundAmount1.Text = "$" + (requestsFund.amount).ToString("###,###.00");
                    }
                    else
                    {
                        tb_sourceFundAmount1.Text = (requestsFund.percentage * 100).ToString("#.##") + "%";
                    }

                }
                else if (i == 2)
                {
                    ddl_sourceFund2.SelectedValue = requestsFund.code;
                    if (requestsFund.percentage == 0)
                    {
                        tb_sourceFundAmount2.Text = "$" + (requestsFund.amount).ToString("###,###.00");
                    }
                    else
                    {
                        tb_sourceFundAmount2.Text = (requestsFund.percentage * 100).ToString("#.##") + "%";
                    }
                }
                else if (i == 3)
                {
                    ddl_sourceFund3.SelectedValue = requestsFund.code;
                    if (requestsFund.percentage == 0)
                    {
                        tb_sourceFundAmount3.Text = "$" + (requestsFund.amount).ToString("###,###.00");
                    }
                    else
                    {
                        tb_sourceFundAmount3.Text = (requestsFund.percentage * 100).ToString("#.##") + "%";
                    }
                }
                else if (i == 4)
                {
                    ddl_sourceFund4.SelectedValue = requestsFund.code;
                    if (requestsFund.percentage == 0)
                    {
                        tb_sourceFundAmount4.Text = "$" + (requestsFund.amount).ToString("###,###.00");
                    }
                    else
                    {
                        tb_sourceFundAmount4.Text = (requestsFund.percentage * 100).ToString("#.##") + "%";
                    }

                }
                else if (i == 5)
                {
                    ddl_sourceFund5.SelectedValue = requestsFund.code;
                    if (requestsFund.percentage == 0)
                    {
                        tb_sourceFundAmount5.Text = "$" + (requestsFund.amount).ToString("###,###.00");
                    }
                    else
                    {
                        tb_sourceFundAmount5.Text = (requestsFund.percentage * 100).ToString("#.##") + "%";
                    }
                }
            }
        }
    }

    private void showRedirectTarget(Requests req)
    {
        int i = 0;
        foreach (RequestsFund requestsFund in req.requestFunds)
        {
            
            string fundType = requestsFund.type;
            if ("REDIRECT_TARGET" == fundType)
            {
                i++;
                if (i == 1)
                {
                    ddl_redirectFund1.SelectedValue = requestsFund.code;
                    tb_redirectFundPercentage1.Text = (requestsFund.percentage * 100).ToString("#.##");
                }
                else if (i == 2)
                {
                    ddl_redirectFund2.SelectedValue = requestsFund.code;
                    tb_redirectFundPercentage2.Text = (requestsFund.percentage * 100).ToString("#.##");
                }
                else if (i == 3)
                {
                    ddl_redirectFund3.SelectedValue = requestsFund.code;
                    tb_redirectFundPercentage3.Text = (requestsFund.percentage * 100).ToString("#.##");
                }
                else if (i == 4)
                {
                    ddl_redirectFund4.SelectedValue = requestsFund.code;
                    tb_redirectFundPercentage4.Text = (requestsFund.percentage * 100).ToString("#.##");
                }
                else if (i == 5)
                {
                    ddl_redirectFund5.SelectedValue = requestsFund.code;
                    tb_redirectFundPercentage5.Text = (requestsFund.percentage * 100).ToString("#.##");
                }


            }

        }


    }

    private void showSwitchTarget(Requests req)
    {
        int i = 0;
        foreach (RequestsFund requestsFund in req.requestFunds)
        { 
            string fundType = requestsFund.type;
            if ("SWITCH_TARGET" == fundType)
            {
                i++;
                if (i == 1)
                {
                    ddl_targetFund1.SelectedValue = requestsFund.code;
                    if (requestsFund.percentage == 0)
                    {
                        tb_targetFundAmount1.Text = Convert.ToString(requestsFund.amount);
                    }
                    else
                    {
                        tb_targetFundAmount1.Text = (requestsFund.percentage * 100).ToString("#.##");
                    }
                }
                else if (i == 2)
                {
                    ddl_targetFund2.SelectedValue = requestsFund.code;
                    if (requestsFund.percentage == 0)
                    {
                        tb_targetFundAmount2.Text = Convert.ToString(requestsFund.amount);
                    }
                    else
                    {
                        tb_targetFundAmount2.Text = (requestsFund.percentage * 100).ToString("#.##");
                    }
                }
                else if (i == 3)
                {
                    ddl_targetFund3.SelectedValue = requestsFund.code;
                    if (requestsFund.percentage == 0)
                    {
                        tb_targetFundAmount3.Text = Convert.ToString(requestsFund.amount);
                    }
                    else
                    {
                        tb_targetFundAmount3.Text = (requestsFund.percentage * 100).ToString("#.##");
                    }
                }
                else if (i == 4)
                {
                    ddl_targetFund4.SelectedValue = requestsFund.code;
                    if (requestsFund.percentage == 0)
                    {
                        tb_targetFundAmount4.Text = Convert.ToString(requestsFund.amount);
                    }
                    else
                    {
                        tb_targetFundAmount4.Text = (requestsFund.percentage * 100).ToString("#.##");
                    }

                }
                else if (i == 5)
                {
                    ddl_targetFund5.SelectedValue = requestsFund.code;
                    if (requestsFund.percentage == 0)
                    {
                        tb_targetFundAmount5.Text = Convert.ToString(requestsFund.amount);
                    }
                    else
                    {
                        tb_targetFundAmount5.Text = (requestsFund.percentage * 100).ToString("#.##");
                    }
                }

            }
        }


    }

    protected void btn_approve_Click(object sender, ImageClickEventArgs e)
    {
        //string policyNo = "FP2013";

        int requestId = Convert.ToInt32(this.Request.QueryString["requestId"]);
        Requests req = Requests.getRequest2(requestId);

        try
        {
            this.switchFund(req.policyNo);
            this.redirectFund(req.policyNo);
            req.approve(req.requestId);
            this.Response.Redirect("./manageClients.aspx");
        }
        catch (RetirementException ex)
        {
            this.lb_exceptionMsg.Text = ex.message;
        }
    }


    protected void btn_reject_Click(object sender, ImageClickEventArgs e)
    {
        int requestId = Convert.ToInt32(this.Request.QueryString["requestId"]);
        Requests req = new Requests();
        req.reject(req.requestId, this.tb_rejectReason.Text);

        this.Response.Redirect("./manageClients.aspx");
    }

    private void switchFund(string policyNo){

        Dictionary<string, string> sourcePercentageOrAmount = new Dictionary<string, string>();
        if (!Util.IsBlank(ddl_sourceFund1.SelectedValue))
        {
            sourcePercentageOrAmount.Add(ddl_sourceFund1.SelectedValue, this.tb_sourceFundAmount1.Text);
        }

        if (!Util.IsBlank(ddl_sourceFund2.SelectedValue))
        {
            sourcePercentageOrAmount.Add(ddl_sourceFund2.SelectedValue, this.tb_sourceFundAmount2.Text);
        }

        if (!Util.IsBlank(ddl_sourceFund3.SelectedValue))
        {
            sourcePercentageOrAmount.Add(ddl_sourceFund3.SelectedValue, this.tb_sourceFundAmount3.Text);
        }

        if (!Util.IsBlank(ddl_sourceFund4.SelectedValue))
        {
            sourcePercentageOrAmount.Add(ddl_sourceFund4.SelectedValue, this.tb_sourceFundAmount4.Text);
        }

        if (!Util.IsBlank(ddl_sourceFund5.SelectedValue))
        {
            sourcePercentageOrAmount.Add(ddl_sourceFund5.SelectedValue, this.tb_sourceFundAmount5.Text);
        }

        Dictionary<string, string> targetPercentage = new Dictionary<string, string>();
        if (!Util.IsBlank(ddl_targetFund1.SelectedValue))
        {
            targetPercentage.Add(ddl_targetFund1.SelectedValue, this.tb_targetFundAmount1.Text);
        }

        if (!Util.IsBlank(ddl_targetFund2.SelectedValue))
        {
            targetPercentage.Add(ddl_targetFund2.SelectedValue, this.tb_targetFundAmount2.Text);
        }

        if (!Util.IsBlank(ddl_targetFund3.SelectedValue))
        {
            targetPercentage.Add(ddl_targetFund3.SelectedValue, this.tb_targetFundAmount3.Text);
        }

        if (!Util.IsBlank(ddl_targetFund4.SelectedValue))
        {
            targetPercentage.Add(ddl_targetFund4.SelectedValue, this.tb_targetFundAmount4.Text);
        }

        if (!Util.IsBlank(ddl_targetFund5.SelectedValue))
        {
            targetPercentage.Add(ddl_targetFund5.SelectedValue, this.tb_targetFundAmount5.Text);
        }

        PolicyRules policyRules = new PolicyRules();

        if (sourcePercentageOrAmount.Keys.Count == 0 && targetPercentage.Keys.Count == 0)
        {
            return;
        }

        decimal totalPercentage = 0;
        foreach (string percentage in targetPercentage.Values)
        {
            totalPercentage += Convert.ToDecimal(percentage);
        }

        if (totalPercentage != 100)
        {
            return;
        }

        policyRules.switchFund(policyNo, sourcePercentageOrAmount, targetPercentage);
        
    }

    private void redirectFund(string policyNo)
    {
        //Redirect funds
        Dictionary<string, string> allocationPercentage = new Dictionary<string, string>();
        if (!Util.IsBlank(ddl_redirectFund1.SelectedValue))
        {
            allocationPercentage.Add(ddl_redirectFund1.SelectedValue, this.tb_redirectFundPercentage1.Text);
        }

        if (!Util.IsBlank(ddl_redirectFund2.SelectedValue))
        {
            allocationPercentage.Add(ddl_redirectFund2.SelectedValue, this.tb_redirectFundPercentage2.Text);
        }

        if (!Util.IsBlank(ddl_redirectFund3.SelectedValue))
        {
            allocationPercentage.Add(ddl_redirectFund3.SelectedValue, this.tb_redirectFundPercentage3.Text);
        }

        if (!Util.IsBlank(ddl_redirectFund4.SelectedValue))
        {
            allocationPercentage.Add(ddl_redirectFund4.SelectedValue, this.tb_redirectFundPercentage4.Text);
        }

        if (!Util.IsBlank(ddl_redirectFund5.SelectedValue))
        {
            allocationPercentage.Add(ddl_redirectFund5.SelectedValue, this.tb_redirectFundPercentage5.Text);
        }

        if (allocationPercentage.Keys.Count == 0 && allocationPercentage.Keys.Count == 0)
        {
            return;
        }

        PolicyRules policyRules = new PolicyRules();

        decimal totalRedirectPercentage = 0;
        foreach (string percentage in allocationPercentage.Values)
        {
            totalRedirectPercentage += Convert.ToDecimal(percentage);
        }

        if (totalRedirectPercentage != 100)
        {
            return;
        }

        policyRules.redirectFund(policyNo, allocationPercentage);
        //Requests requests = new Requests();
        //string requestIdstr = (string)Request.QueryString["requestId"];
        //int requestId = Convert.ToInt32(requestIdstr);
        //requests.approve(requestId);
    }
}